package cn.bellychang.ood.atmsimulator.model;

import cn.bellychang.ood.atmsimulator.CardReaderEnum;
import cn.bellychang.ood.atmsimulator.Notifier;
import cn.bellychang.ood.atmsimulator.Observer;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ChangLiang
 * @date 2020/5/21
 */
@Data
public class CardReader implements Notifier {

    List<Observer> observers = new ArrayList<>();

    /**
     * 需要观察的状态
     */
    private CardReaderEnum action;

    private String cardNo;

    @Override
    public void attach(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void detach(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void notifier() {
        observers.forEach(observer -> observer.update(this.action, this.cardNo));
    }

    public void readCardInfo(String cardNo){
        this.action = CardReaderEnum.INSERT_CARD;
        this.cardNo = cardNo;
        this.notifier();
    }

    public void ejectCard(){
        this.action = CardReaderEnum.EJECT_CARD;
        this.notifier();
    }

    public void eatCard(){
        this.action = CardReaderEnum.EAT_CARD;
        this.notifier();
    }
}
